package org.example.springbook.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.springbook.constant.Constants;
import org.example.springbook.enums.BookStatusEnum;
import org.example.springbook.model.BookInfo;
import org.example.springbook.model.PageRequest;
import org.example.springbook.model.PageResponse;
import org.example.springbook.model.Result;
import org.example.springbook.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;



    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())) {
            return "参数错误";
        }
        try {
            Integer result = bookService.insertBook(bookInfo);
            if (result == 1) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "添加失败";
    }
@RequestMapping("/getListByPage")
    public Result<PageResponse<BookInfo>> getListBypage(PageRequest pageRequest, HttpServletRequest request) {
        log.info("获取图书列表,pageRequest:{}",pageRequest);
//        HttpSession session=request.getSession(false);
//        if(session==null||session.getAttribute(Constants.SESSION_USER_KET)==null ||!StringUtils.hasLength((String) session.getAttribute(Constants.SESSION_USER_KET))){
//            return Result.unlogin();
//        }
        PageResponse<BookInfo> pageResponse=new PageResponse<>();
        try {
            pageResponse = bookService.getListByPage(pageRequest);
        }catch (Exception e){
            log.info("获取图书列表失败，e:",e);
            return Result.fail();
        }
        return Result.success(pageResponse);
    }
@RequestMapping("queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("获取图书信息,bookId"+bookId);
        if(bookId==null||bookId<=0){
            return null;
        }
        return bookService.queryBookById(bookId);

    }
    @RequestMapping(value = "updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书，bookInfo:{}",bookInfo);
        try {
          Integer result=  bookService.updateBook(bookInfo);
          if(result>0){
              return "";
          }
          return "图书更新失败";
          } catch (Exception e) {
            log.info("更新图书失败，e:",e);
            return "数据库操作失败";
        }
        }
    @RequestMapping(value = "deleteBook",produces = "application/json")
    public String deltyeBook(Integer bookId){
        log.info("删除图书，bookId:{}",bookId);
        try {
            BookInfo bookInfo=new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
            Integer result=  bookService.updateBook(bookInfo);
            if(result>0){
                return "";
            }
            return "图书删除失败";
        } catch (Exception e) {
            log.info("删除图书失败，e:",e);
            return "数据库操作失败";
        }
    }
    @RequestMapping("/batchDeleteBook")
    public Boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书，ids:{}",ids);
        try{
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e) {
            log.info("批量删除图书失败，ids:{}:",ids);

        }
        return false;
    }
    }

